﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;

namespace Client.ValueConverters
{
    // V 1.0.1 - 2022-10-30 14:47:59
    // 修复 parameter 判断逻辑的bug
    // 
    // V 1.0.0 - 2022-10-30 14:48:59
    // 首次创建

    /// <summary>
    /// DateConverter, 日期转换器, 默认转换格式 yyyy-MM-dd, 可使用 parameter 传递自定义格式
    /// </summary>
    public class DateConverter : System.Windows.Data.IValueConverter
    {
        /// <summary>
        /// Convert, 默认 StringFormat: yyyy-MM-dd
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string format = "yyyy-MM-dd";

            // parameter 非空, format 的值等于 parameter
            if (parameter != null && string.IsNullOrWhiteSpace(parameter.ToString()) == false)
            {
                format = parameter.ToString();
            }

            return ((DateTime)value).ToString(format);
        }

        /// <summary>
        /// ConvertBack
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
    }
}
